<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="cancelDialog">
    <el-form
      ref="form"
      :model="form"
      :rules="addRules"
      label-width="80px"
      style="width:75%;margin:0 auto"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="form.username"
          autocomplete="off"
          placeholder="请输入姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="form.mobile"
          autocomplete="off"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          style="width:100%"
          v-model="form.timeOfEntry"
          type="date"
          placeholder="请选择入职时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="form.formOfEmployment"
          placeholder="请选择聘用形式"
          style="width:100%"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="form.workNumber"
          autocomplete="off"
          placeholder="请输入工号"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          @focus="showTreeData"
          @blur="handleBlur"
          v-model="form.departmentName"
          autocomplete="off"
          placeholder="请选择部门"
        ></el-input>
        <el-tree
          v-if="treeShow"
          v-loading="treeLoading"
          :data="departments"
          :props="defaultProps"
          default-expand-all
          @node-click="nodeClick"
        ></el-tree>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          style="width:100%"
          v-model="form.correctionTime"
          type="date"
          placeholder="请选择转正时间"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelDialog">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 获取部门接口
import { getDepartments } from '@/api/departments'
// 添加员工的接口
import { addEmpList } from '@/api/employees'
// 封装的树状函数
import { transToTreeData } from '@/utils/index.js'
// 枚举库
import enumObj from '@/api/constant/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      hireType: enumObj.hireType,
      // 表单 绑定数据
      form: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      // 表单校验规则
      addRules: {
        username: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' },
          { min: 2, max: 4, message: '请输入2-4个字符', trigger: 'change' }
        ],
        mobile: [
          { required: true, message: '请输入员工手机号', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择员工入职时间', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择员工聘用形式', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '请输入员工工号', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请选择员工部门名称', trigger: 'change' }
        ],
        correctionTime: [
          { required: true, message: '请选择员工转正时间', trigger: 'blur' }
        ]
      },
      // 树状控件绑定数据
      departments: [],
      // 指定树的文字和子节点是什么属性
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      // 设置树形结构显示或隐藏的属性
      treeShow: false,
      treeLoading: false
    }
  },
  methods: {
    // 文本框获取焦点事件
    async showTreeData () {
      // 显示树形结构
      this.treeShow = true
      this.treeLoading = true
      let res = await getDepartments()
      this.departments = transToTreeData(res.data.depts, '')
      this.treeLoading = false
    },
    // 参数1:被点节点的数据
    // 参数2:被点节点这个对象
    // 参数3:被点节点所在tree组件
    nodeClick (data, node, tree) {
      // 把被点的数据赋值给文本框绑定的数据
      this.form.departmentName = data.name
      this.treeShow = false
    },
    handleBlur (e) {
      // 如果失去焦点去的不是tree,那么隐藏tree
      if (e.relatedTarget.className.indexOf('el-tree-node') == -1) {
        this.treeShow = false
      }
    },
    save () {
      this.$refs.form.validate(async v => {
        if (v) {
          await addEmpList(this.form)
          this.$message.success('新增员工成功')
          this.$emit('update:showDialog', false)
          // math.ceil((总数据+1)/总容量 )
          let pageInfo = this.$parent.pageInfo
          pageInfo.total += 1
          let lastPage = Math.ceil(pageInfo.total / pageInfo.size)
          this.$parent.pageInfo.page = lastPage
          this.$parent.getEmpList()
        }
      })
    },
    cancelDialog () {
      this.from = {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      }
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style></style>
